package com.sskj.mine;

import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.mine.bean.ShellRecordBean;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;


/**
 * A simple {@link Fragment} subclass.
 */
@Route(path = RConfig.MINE_SHELL_RECORD)//出金记录
public class ShellRecordFragment extends BaseFragment<ShellRecordPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    private SlimAdapter<ShellRecordBean> slimAdapter;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCHelper mvcHelper;
    private HashMap<String, String> statusMap;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shell_record;
    }

    @Override
    protected ShellRecordPresenter getPresenter() {
        return new ShellRecordPresenter();
    }

    @Override
    protected void initView() {
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        statusMap = new HashMap<>();
        statusMap.put("1", getString(R.string.mine_out1));
        statusMap.put("2", getString(R.string.mine_out2));
        statusMap.put("3", getString(R.string.mine_out3));
        statusMap.put("4", getString(R.string.mine_out4));
        statusMap.put("5", getString(R.string.mine_out5));

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.mine_1c1))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_buyshellrecord, new SlimInjector<ShellRecordBean>() {
            @Override
            public void onInject(ShellRecordBean data, IViewInjector iViewInjector, List payloads) {

                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                String abc = data.getPrice();
                String format1 = decimalFormat.format(new BigDecimal(abc));
                iViewInjector.text(R.id.tvMoney, format1);
                iViewInjector.text(R.id.tvState, statusMap.get(data.getState()));
                Date date = new Date(Long.parseLong(data.getAddtime()) * 1000);
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                String replace = format.replace(" ", "\n");
                iViewInjector.text(R.id.tvTime, replace);
            }
        });

        slimAdapter.attachTo(recyclerView).updateData(new ArrayList());

        coolRefreshView.setPullHeader(new MaterialHeader(getActivity()));
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);

        ModelRx2DataSource refreshDataSource = new ModelRx2DataSource<ShellRecordBean>(
                (ModelRx2DataSource.OnLoadSource<ShellRecordBean>)
                        (int page) -> {
                            return mPresenter.getRecordList(page, 10);
                        }, 10);
        // 设置数据源
        mvcHelper.setDataSource(refreshDataSource);
        // 设置适配器
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

}
